﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimKen.Web.User
{
    public partial class UserAccountEdit : TimKen.App.WebPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (CurrentUserName == null || CurrentUserName == "")
                {
                    Alert("您必须登录！", "../Index.aspx");
                }                           
                BindWebUserInfo();
                BindData();
                if (Request.QueryString["type"] != null)
                {
                    Alert(GetLangString("PasswordExpiredMsg"));
                }
            }
        }

        private void BindWebUserInfo()
        {
            TimKen.BussLogic.UserAccountManager userM = new TimKen.BussLogic.UserAccountManager();
            TimKen.DataEntity.UserAccount user = userM.GetModel(CurrentUserName);
            if (user != null)
            {
                TimKen.BussLogic.CustomerManager customM = new TimKen.BussLogic.CustomerManager();
                TimKen.DataEntity.Customer custom = customM.GetModel(user.CustomerID.Value);
                if (custom != null)
                {
                    labCustomerName.Text = custom.CustomerName;
                    labCustomerID.Value = custom.CustomerID.ToString();
                    labEmail.Text = user.Email;
                    labRealName.Text = user.RealName;
                    labTelephone.Text = custom.Telephone;
                    labUserName.Text = user.UserName;
                }
                else
                {
                    btnUpdate.Visible = false;
                    Response.Redirect("/ErrorAuthority.aspx");
                }
            }
            else
            {
                btnUpdate.Visible = false;
                Response.Redirect("/ErrorAuthority.aspx");
            }
        }

        protected void BindData()
        {
            TimKen.BussLogic.UserAccountManager userM = new TimKen.BussLogic.UserAccountManager();
            repUserAccount.DataSource = userM.GetUserAccountPageList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "-1", labCustomerID.Value, "", LanguageID);
            repUserAccount.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtPassword.Text == "")
            {
                return;
            }
            TimKen.BussLogic.UserAccountManager userM = new TimKen.BussLogic.UserAccountManager();
            TimKen.DataEntity.UserAccount user = userM.GetModel(CurrentUserName);
            if (user == null)
            {
                Alert(GetLangString("NoAllowUpdateUserInfo"));
            }
            else
            {
                user.LastUpdatePwd = DateTime.Now;
                user.Password = txtPassword.Text;
                try
                {
                    userM.Update(user);
                }
                catch
                {
                    Alert(GetLangString("UpdateError"));
                }
            }
        }

    }
}
